import hosts from './hosts'
import apis from './apis'

// ENV
// const env = 'development' // 'development' or 'production'
const env = 'test' // 'development' or 'production'
// const env = 'production' // 'development' or 'production'

const debug = true

const host = hosts['init'][env]

module.exports = {
  api: disposeUrl(apis, host)
}

function disposeUrl (obj, prefix) {
  Object.keys(obj).forEach(v => {
    if (obj[v].url) {
      obj[v].url = prefix + obj[v].url
      obj[v].apiConfigPre = apiConfigPre
    } else {
      obj[v] = disposeUrl(obj[v], prefix)
    }
    if (debug) {
      obj[v].XDEBUG_SESSION = 'PHPSTORM'
    }
  })

  return obj
}

function apiConfigPre() {
  if (env === 'production' || env === 'test') {
    if (this.preg) {
      for (let key in this.preg) {
        this.url = this.url.replace('{' + key + '}', this.preg[key])
      }
    }
  }
  return this
}
